/*
 *  Monitor.c - Interface between TW_Terminal2 and Firmware.
 *
 *  Copyright (C) 2011 Intersil Corporation
 *
 */

//*****************************************************************************
//
//								Monitor.c
//
//*****************************************************************************
//
//
//#include <intrins.h>

#include "config.h"
#include "reg.h"
#include "typedefs.h"
#include "TW8835.h"

#include "cpu.h"
#include "util.h"
#include "printf.h"			  
#include "i2c.h"
#include "monitor.h"
#include "main.h"


		BYTE 	DebugLevel = 0;
XDATA	BYTE	MonAddress = TW88I2CAddress;	
XDATA	BYTE	MonIndex;
XDATA	BYTE	MonRdata, MonWdata;
XDATA	BYTE	monstr[40];				// buffer for input string
XDATA	BYTE 	*argv[10];
XDATA	BYTE	argc=0;
		bit		echo=1;
		bit		access=1;

XDATA	BYTE	mon1str[40];				// buffer for input string
XDATA	BYTE 	*argv1[10];
XDATA	BYTE	argc1=0;

extern			bit		RS1_Xbusy;
//=============================================================================
//
//=============================================================================
void Prompt(void)
{
#ifdef BANKING
	if ( MonAddress == TW88I2CAddress )
		Printf("\n[B%02bx]MCU_I2C[%02bx]>", BANKREG, MonAddress);
	else
#else
	if ( MonAddress == TW88I2CAddress )
		Printf("\nMCU_I2C[%02bx]>", MonAddress);
	else
#endif
		Printf("\nI2C[%02bx]>", MonAddress);
}


void Mon_tx(BYTE ch)
{
	RS_tx(ch);
}
#ifdef SUPPORT_UART1
void Mon1_tx(BYTE ch)
{
	RS1_tx(ch);
}
#endif
//=============================================================================
//
//=============================================================================

void SetMonAddress(BYTE addr)
{
	MonAddress = addr;
}

void MonReadI2C(void)
{
	////////////////////////////////////
	BYTE len;
			
	if( argc>=2 ) for(len=0; len<10; len++) if( argv[1][len]==0 ) break;

	if( len>2 ) {
		MonWdata = a2h( argv[1] ) / 0x100;
		MonIndex = 0xff;

		Printf("\nWrite %02bxh:%02bxh ", MonIndex, MonWdata);
		if ( MonAddress == TW88I2CAddress ) {
			WriteTW88(MonIndex, MonWdata);
			MonRdata = ReadTW88(MonIndex);
		}
		else {
			WriteI2C(MonAddress, MonIndex, MonWdata);
			MonRdata = ReadI2C(MonAddress, MonIndex);
		}
   		Printf("==> Read %02bxh:%02bxh", MonIndex, MonRdata);
		if( MonWdata != MonRdata ) Printf(" [%02bx]", MonWdata);
	}
	////////////////////////////////////

	if( argc>=2 ) MonIndex = a2h( argv[1] );
	else	{
		Printf("   --> Missing parameter !!!");
		return;
	}

	if ( MonAddress == TW88I2CAddress )
		MonRdata = ReadTW88(MonIndex);
	else
		MonRdata = ReadI2C(MonAddress, MonIndex);

	if( echo )
		Printf("\nRead %02bxh:%02bxh", MonIndex, MonRdata);	
	
	MonWdata = MonRdata;
}


void MonWriteI2C(void) 
{
	////////////////////////////////////
	BYTE len;

	if( argc>=2 ) for(len=0; len<10; len++) if( argv[1][len]==0 ) break;

	if( len>2 ) {
		MonWdata = a2h( argv[1] ) / 0x100;
		MonIndex = 0xff;

		Printf("\nWrite %02bxh:%02bxh ", MonIndex, MonWdata);
		if ( MonAddress == TW88I2CAddress ) {
			if(MonIndex==0xff) 	{ WriteTW88Page(MonWdata); }
			else				WriteTW88(MonIndex, MonWdata);
			MonRdata = ReadTW88(MonIndex);
		}
		else {
			WriteI2C(MonAddress, MonIndex, MonWdata);
			MonRdata = ReadI2C(MonAddress, MonIndex);
		}
   		Printf("==> Read %02bxh:%02bxh", MonIndex, MonRdata);
		if( MonWdata != MonRdata ) Printf(" [%02bx]", MonWdata);
	}
	////////////////////////////////////

	if( argc<3 ) {
		Printf("   --> Missing parameter !!!");
		return;
	}
	
	MonIndex = a2h( argv[1] );
	MonWdata = a2h( argv[2] );
	
	if( echo ) {
		Printf("\nWrite %02bxh:%02bxh ", MonIndex, MonWdata);
		if ( MonAddress == TW88I2CAddress ) {
			if(MonIndex==0xff) 	{ WriteTW88Page(MonWdata); }
			else				WriteTW88(MonIndex, MonWdata);
			MonRdata = ReadTW88(MonIndex);
		}
		else {
			WriteI2C(MonAddress, MonIndex, MonWdata);
			MonRdata = ReadI2C(MonAddress, MonIndex);
		}
   		Printf("==> Read %02bxh:%02bxh", MonIndex, MonRdata);
		if( MonWdata != MonRdata ) Printf(" [%02bx]", MonWdata);
	}
	else {
		if ( MonAddress == TW88I2CAddress ) {
			if(MonIndex==0xff) 	{ WriteTW88Page(MonWdata); }
			else				WriteTW88(MonIndex, MonWdata);
		}
		else {
			WriteI2C(MonAddress, MonIndex, MonWdata);
		}
	}
}


void MonIncDecI2C(BYTE inc)
{
	switch(inc){
		case 0:  MonWdata--;	break;
		case 1:  MonWdata++;	break;
		case 10: MonWdata-=0x10;	break;
		case 11: MonWdata+=0x10;	break;
	}

	if ( MonAddress == TW88I2CAddress ) {
		WriteTW88(MonIndex, MonWdata);
		MonRdata = ReadTW88(MonIndex);
	}
	else {
		WriteI2C(MonAddress, MonIndex, MonWdata);
		MonRdata = ReadI2C(MonAddress, MonIndex);
	}

	if( echo ) {
		Printf("Write %02bxh:%02bxh ", MonIndex, MonWdata);
		Printf("==> Read %02bxh:%02bxh", MonIndex, MonRdata);
	}

	Prompt();

}

void MonDumpI2C(void)
{
	BYTE ToMonIndex;
	int  cnt=7;

	if( argc>=2 ) MonIndex   = a2h(argv[1]);
	if( argc>=3 ) ToMonIndex = a2h(argv[2]);
	else          ToMonIndex = MonIndex+cnt;
	
	if ( ToMonIndex < MonIndex ) ToMonIndex = 0xFF;
	cnt = ToMonIndex - MonIndex + 1;

	if( echo ) {
		if ( MonAddress == TW88I2CAddress ) {
			for ( ; cnt > 0; cnt-- ) {
				MonRdata = ReadTW88(MonIndex);
				Printf("\n==> Read %02bxh:%02bxh", MonIndex, MonRdata);
				MonIndex++;
			}
		}
		else {
			for ( ; cnt > 0; cnt-- ) {
				MonRdata = ReadI2C(MonAddress, MonIndex);
				Printf("\n==> Read %02bxh:%02bxh", MonIndex, MonRdata);
				//if( MonWdata != MonRdata ) Printf(" [%02bx]", MonWdata);
				MonIndex++;
			}
		}
	}
	else {
		if ( MonAddress == TW88I2CAddress ) {
			for ( ; cnt > 0; cnt-- ) {
				MonRdata = ReadTW88(MonIndex);
				MonIndex++;
			}
		}
		else {
			for ( ; cnt > 0; cnt-- ) {
				MonRdata = ReadI2C(MonAddress, MonIndex);
				MonIndex++;
			}
		}
	}
}

//-----------------------------------------------------------------------------

void MonNewReadI2C(void)
{
	BYTE Slave;

	if( argc>=3 ) MonIndex = a2h( argv[2] );
	else	{
		Printf("   --> Missing parameter !!!");
		return;
	}
	Slave = a2h(argv[1]);

	if ( Slave == TW88I2CAddress )
		MonRdata = ReadTW88(MonIndex);
	else
		MonRdata = ReadI2C(Slave, MonIndex);

	if( echo )
		Printf("\n<R>%02bx[%02bx]=%02bx", Slave, MonIndex, MonRdata);
	
	MonWdata = MonRdata;
}

void MonNewWriteI2C(void)
{
	BYTE Slave;

	if( argc<4 ) {
		Printf("   --> Missing parameter !!!");
		return;
	}
	
	MonIndex = a2h( argv[2] );
	MonWdata = a2h( argv[3] );
	Slave    = a2h( argv[1] );
	
	if ( Slave == TW88I2CAddress ) {
		if(MonIndex==0xff) 	{ WriteTW88Page(MonWdata); }
		else				WriteTW88(MonIndex, MonWdata);
		MonRdata = ReadTW88(MonIndex);
	}
	else {
		WriteI2C(Slave, MonIndex, MonWdata);
		MonRdata = ReadI2C(Slave, MonIndex);
   	}
	if( echo ) {
		Printf("\n<R>%02bx[%02bx]=%02bx", Slave, MonIndex, MonRdata);
	}

}

void MonNewDumpI2C(void)
{
	BYTE 	ToMonIndex, Slave;
	WORD	i;
	
	if( argc>=2 ) MonIndex = a2h(argv[2]);
	if( argc>=3 ) ToMonIndex = a2h(argv[3]);
	Slave = a2h(argv[1]);

	if( echo ) {
		if ( Slave == TW88I2CAddress ) {
			for(i=MonIndex; i<=ToMonIndex; i++) {
				MonRdata = ReadTW88(i);
        		Printf("\n<R>%02bx[%02x]=%02bx", Slave, i, MonRdata);
			}
		}
		else {
			for(i=MonIndex; i<=ToMonIndex; i++) {
				MonRdata = ReadI2C(Slave, i);
        		Printf("\n<R>%02bx[%02x]=%02bx", Slave, i, MonRdata);
			}
		}
	}
	else {
		if ( Slave == TW88I2CAddress ) {
			for(i=MonIndex; i<=ToMonIndex; i++) {
				MonRdata = ReadTW88(i);
			}
		}
		else {
			for(i=MonIndex; i<=ToMonIndex; i++) {
				MonRdata = ReadI2C(Slave, i);
			}
		}
	}
}


void MonWriteBit(void)
{
	BYTE mask, i, FromBit, ToBit,  MonMask, val;
	BYTE Slave;
	// b 88 index startbit|endbit data

	if( argc<5 ) {
		Printf("   --> Missing parameter !!!");
		return;
	}
	Slave = a2h(argv[1]);

	MonIndex = a2h( argv[2] );
	FromBit  = (a2h( argv[3] ) >> 4) & 0x0f;
	ToBit  = a2h( argv[3] )  & 0x0f;
	MonMask  = a2h( argv[4] );

	if( FromBit<ToBit || FromBit>7 || ToBit>7) {
		Printf("\n   --> Wrong range of bit operation !!!");
		return;
	}
	
	mask = 0xff; 
	val=0x7f;
	for(i=7; i>FromBit; i--) {
		mask &= val;
		val = val>>1;
	}

	val=0xfe;
	for(i=0; i<ToBit; i++) {
		mask &= val;
		val = val<<1;
	}

	if ( Slave == TW88I2CAddress ) {
		MonRdata = ReadTW88(MonIndex);
		MonWdata = (MonRdata & (~mask)) | (MonMask & mask);
				
		WriteTW88(MonIndex, MonWdata);
		MonRdata = ReadTW88(MonIndex);
	}
	else {
		MonRdata = ReadI2C(Slave, MonIndex);
		MonWdata = (MonRdata & (~mask)) | (MonMask & mask);
				
		WriteI2C(Slave, MonIndex, MonWdata);
		MonRdata = ReadI2C(Slave, MonIndex);
	}

	if( echo )
		Printf("\n<R>%02bx[%02bx]=%02bx", Slave, MonIndex, MonRdata);

}
// wait reg mask result max_wait
void MonWait(void)
{
	WORD i,max;
	BYTE reg, mask, result;
	if( argc<5 ) {
		Printf("   --> Missing parameter !!!");
		return;
	}
	reg = a2h( argv[1] );
	mask = a2h( argv[2] );
	result = a2h( argv[3] );
	max = a2h( argv[4] );
	for(i=0; i < max; i++) {
		if((ReadTW88(reg) & mask)==result) {
			Printf("=>OK@%bd",i);
			break;
		}
		delay1ms(2);
	}
	if(i >= max)
		Printf("=>fail wait %bx %bx %bx %d->fail",reg,mask,result,max);
}

//=============================================================================
//			Help Message
//=============================================================================
void MonHelp(void)
{
	Puts("\n=======================================================");
	Puts("\n>>>     Welcome to Intersil Monitor  Rev 1.01       <<<");
	Puts("\n=======================================================");
	Puts("\n   R ii             ; Read data.(");
	Puts("\n   W ii dd          ; Write data.)");
	Puts("\n   D [ii] [cc]      ; Dump.&");
	Puts("\n   B AA II bb DD    ; Bit Operation");
	Puts("\n   C aa             ; Change I2C address");
	Puts("\n   Echo On/Off      ; Terminal Echoing On/Off");
	Puts("\n=======================================================");
	Puts("\n=== DEBUG ACCESS time init MCU SPI EE menu task [on] ====");
	Puts("\n=======================================================");
	Puts("\n");

}
#ifdef SUPPORT_UART1
void Mon1Help(void)
{
	Puts("\n=======================================================");
	Puts("\n>>>     Welcome to Intersil Monitor1  Rev 1.01       <<<");
	Puts("\n=======================================================");
	Puts("\n=======================================================");
	//Puts("\n=== DEBUG ACCESS time init MCU SPI EE menu task [on] ====");
	Puts("\n=======================================================");
	Puts("\n");

}
#endif
//=============================================================================
//
//=============================================================================
BYTE MonGetCommand(void)
{
	static BYTE comment=0;
	static BYTE incnt=0, last_argc=0;
	BYTE i, ch;
	BYTE ret=0;

	if( !RS_ready() ) return 0;
	ch = RS_rx();

	//----- if comment, echo back and ignore -----
	if( comment ) {
		if( ch=='\r' || ch==0x1b ) comment = 0;
		else { 
			Mon_tx(ch);
			return 0;
		}
	}
	else if( ch==';' ) {
		comment = 1;
		Mon_tx(ch);
		return 0;
	}

	//=====================================
	switch( ch ) {

	case 0x1b:
		argc = 0;
		incnt = 0;
		comment = 0;
		Prompt();
		return 0;

	//--- end of string
	case '\r':

		if( incnt==0 ) {
			Prompt();
			break;
		}

		monstr[incnt++] = '\0';
		argc=0;

		for(i=0; i<incnt; i++) if( monstr[i] > ' ' ) break;

		if( !monstr[i] ) {
			incnt = 0;
			comment = 0;
			Prompt();
			return 0;
		}
		argv[0] = &monstr[i];
		for(; i<incnt; i++) {
			if( monstr[i] <= ' ' ) {
				monstr[i]='\0';
     			 //Printf("(%s) ",  argv[argc]);
				i++;
				while( monstr[i]==' ' ) i++;
				argc++;
				if( monstr[i] ){
     			 argv[argc] = &monstr[i];
				}
			}
		}

		ret = 1;
		last_argc = argc;
		incnt = 0;
		
		break;

	//--- repeat command
	case '/':
		argc = last_argc;
		ret = 1;
		break;

	//--- repeat command without CR
	case '`':
	{
		BYTE i, j, ch;

		for(j=0; j<last_argc; j++) {
			for(i=0; i<100; i++) {
				ch = argv[j][i];
				if( ch==0 ) {
					if( j==last_argc-1 ) return 0;
					ch = ' ';
					RS_ungetch( ch );
					break;
				}
				else {
					RS_ungetch( ch );
				}
			}
		}
		break;
	}

	//--- back space
	case 0x08:
		if( incnt ) {
			incnt--;
			Mon_tx(ch);
			Mon_tx(' ');
			Mon_tx(ch);
		}
		break;

	//--- decreamental write
	case ',':
		if( incnt ) {
			Mon_tx(ch);
			monstr[incnt++] = ch;
		}
		else
			MonIncDecI2C(0);
		break;

	case '<':
		if( incnt ) {
			Mon_tx(ch);
			monstr[incnt++] = ch;
		}
		else
			MonIncDecI2C(10);
		break;

	//--- increamental write
	case '.':
		if( incnt ) {
			Mon_tx(ch);
			monstr[incnt++] = ch;
		}
		else
			MonIncDecI2C(1);
		break;

	case '>':
		if( incnt ) {
			Mon_tx(ch);
			monstr[incnt++] = ch;
		}
		else
			MonIncDecI2C(11);
		break;

	default:
		Mon_tx(ch);
		monstr[incnt++] = ch;
		break;
	}

	if( ret ) {
		comment = 0;
		last_argc = argc;
		return ret;
	}
	else {
		return ret;
	}
}

#ifdef SUPPORT_UART1
BYTE Mon1GetCommand(void)
{
	static BYTE comment1=0;
	static BYTE incnt1=0, last_argc1=0;
	BYTE i, ch;
	BYTE ret=0;

	if( !RS1_ready() ) return 0;
	ch = RS1_rx();

	//----- if comment, echo back and ignore -----
	if( comment1 ) {
		if( ch=='\r' || ch==0x1b ) comment1 = 0;
		else { 
			Mon1_tx(ch);
			return 0;
		}
	}
	else if( ch==';' ) {
		comment1 = 1;
		Mon1_tx(ch);
		return 0;
	}

	//=====================================
	switch( ch ) {

	case 0x1b:
		argc1 = 0;
		incnt1 = 0;
		comment1 = 0;
		Prompt();
		return 0;

	//--- end of string
	case '\r':

		if( incnt1==0 ) {
			Prompt();
			break;
		}

		mon1str[incnt1++] = '\0';
		argc1=0;

		for(i=0; i<incnt1; i++) if( mon1str[i] > ' ' ) break;

		if( !mon1str[i] ) {
			incnt1 = 0;
			comment1 = 0;
			Prompt();
			return 0;
		}
		argv1[0] = &mon1str[i];
		for(; i<incnt1; i++) {
			if( mon1str[i] <= ' ' ) {
				mon1str[i]='\0';
     			 //Printf("(%s) ",  argv[argc]);
				i++;
				while( mon1str[i]==' ' ) i++;
				argc1++;
				if( mon1str[i] ){
     			 argv1[argc1] = &mon1str[i];
				}
			}
		}

		ret = 1;
		last_argc1 = argc1;
		incnt1 = 0;
		
		break;

	//--- repeat command
	case '/':
		argc1 = last_argc1;
		ret = 1;
		break;

	//--- repeat command without CR
	case '`':
	{
		BYTE i, j, ch;

		for(j=0; j<last_argc1; j++) {
			for(i=0; i<100; i++) {
				ch = argv1[j][i];
				if( ch==0 ) {
					if( j==last_argc1-1 ) return 0;
					ch = ' ';
					RS1_ungetch( ch );
					break;
				}
				else {
					RS1_ungetch( ch );
				}
			}
		}
		break;
	}

	//--- back space
	case 0x08:
		if( incnt1 ) {
			incnt1--;
			Mon1_tx(ch);
			Mon1_tx(' ');
			Mon1_tx(ch);
		}
		break;

	//--- decreamental write
	case ',':
		if( incnt1 ) {
			Mon1_tx(ch);
			mon1str[incnt1++] = ch;
		}
		//else
		//	MonIncDecI2C(0);	  //BKTODO??
		break;

	case '<':
		if( incnt1 ) {
			Mon1_tx(ch);
			mon1str[incnt1++] = ch;
		}
		//else
		//	MonIncDecI2C(10);	//??BKTODO??
		break;

	//--- increamental write
	case '.':
		if( incnt1 ) {
			Mon1_tx(ch);
			mon1str[incnt1++] = ch;
		}
		//else
		//	MonIncDecI2C(1);	//??BKTODO
		break;

	case '>':
		if( incnt1 ) {
			Mon1_tx(ch);
			mon1str[incnt1++] = ch;
		}
		//else
		//	MonIncDecI2C(11);	//BKTODO
		break;

	default:
		Mon1_tx(ch);
		mon1str[incnt1++] = ch;
		break;
	}

	if( ret ) {
		comment1 = 0;
		last_argc1 = argc1;
		return ret;
	}
	else {
		return ret;
	}
}
#endif

//*****************************************************************************
//				Monitoring Command
//*****************************************************************************

BYTE *MonString = 0;
extern CODE BYTE DataInitADC[];
extern CODE BYTE DataInitYUV[];
extern CODE BYTE DataInitNTSC[];
extern CODE BYTE DataInitDTV[];
extern CODE BYTE DataInitTCON[];

void Monitor(void)
{
	WORD wValue;

	if( MonString ) {
		RS_ungetch( *MonString++ );
		if( *MonString==0 ) MonString = 0;
	}

	if( !MonGetCommand() ) return;

	//---------------- Write Register -------------------
	if( !stricmp( argv[0], "W" ) ) {
		MonWriteI2C();
	}
	else if( !stricmp( argv[0], ")" ) ) {
		MonNewWriteI2C();
	}
	//---------------- Read Register --------------------
	else if ( !stricmp( argv[0], "R" ) ) {
		MonReadI2C();
	}
	else if ( !stricmp( argv[0], "(" ) ) {
		MonNewReadI2C();
	}
	//---------------- Dump Register --------------------
	else if( !stricmp( argv[0], "D" ) ) {
		Puts("\ndump start");
		MonDumpI2C();
	}
	else if( !stricmp( argv[0], "&" ) ) {
		MonNewDumpI2C();
	}

	//---------------- Bit Operation --------------------
	else if( !stricmp( argv[0], "B" ) ) {// Write bits - B AA II bb DD
		MonWriteBit();
	}
	//---------------- Change I2C -----------------------
	else if( !stricmp( argv[0], "C" ) ) {
		MonAddress = a2h( argv[1] );
	}
	//---------------- wait -----------------------
	else if( !stricmp( argv[0], "wait" ) ) {
		MonWait();
	}
	//---------------- delay -----------------------
	else if( !stricmp( argv[0], "delay" ) ) {
		wValue = a2i( argv[1] );
		delay1ms(wValue);
	}

	//---------------- Help -----------------------------
	else if( !stricmp( argv[0], "H" ) || !stricmp( argv[0], "HELP" ) || !stricmp( argv[0], "?" ) ) {
		MonHelp();

	}
	//---------------------------------------------------
/*
	else if( !stricmp( argv[0], "*" ) ) {
			
				if( argc==1 ) {
					Printf("\n  * 0 : Program default Loader");
					Printf("\n  * 1 : Program external Loader");
					Printf("\n  * 2 : Execute Loader");
				}
				else { 
					BYTE mode;
					mode = a2h(argv[1]);
					//Loader(mode);
				}
	}
*/
#if 0
	//---------------- Input Mode ---------------------
	else if ( !stricmp( argv[0], "M" ) ) {
		if ( !stricmp( argv[1], "YUV" ) || !stricmp( argv[1], "Y" )) {
			I2CDeviceInitialize( DataInitYUV, 0 );
			CheckAndSetYUV();		
		}
		else if ( !stricmp( argv[1], "DTV" ) || !stricmp( argv[1], "D" )) {
			I2CDeviceInitialize( DataInitDTV, 0 );
			Printf("\nDVI Input preiod: %d", CheckAndSetDVI());
		}
		else if ( !stricmp( argv[1], "PC" ) || !stricmp( argv[1], "P" )) {
			#ifdef DEV
			I2CDeviceInitialize( DataInitTCON, 0 );
			#endif
			I2CDeviceInitialize( DataInitADC, 0 );
			CheckAndSetPC();
			/*
			InputSetSource(INPUT_PATH_VADC, INPUT_FORMAT_RGB);					// RGB, YUV path
			period = CheckAndSetPC(0);
			Printf("\nRGB input Period: %d", period );
			*/
		}
		else if ( !stricmp( argv[1], "CVBS" ) || !stricmp( argv[1], "C" )) {
			#ifdef DEV
			I2CDeviceInitialize( DataInitNTSC, 0 );
			#endif
		/*
			InitCVBS( );
			WriteTW88( 0xff, 0x01 );		// set Decoder page
			mode = ReadTW88( 0x1c );
			mode >>= 4;
			if ( mode == 0 ) {				// NTSC
			}
			else if ( mode == 1 ) {
				WriteTW88( 0x07, ( ReadTW88( 0x07 ) | 0x10 ) );
				WriteTW88( 0x09, 32 );
				WriteTW88( 0xff, 4 );
				WriteTW88( 0x36, 85 );
			}
			else {
				Printf( "\nInput Mode %bd does not support now", mode );
			}
		*/
		}
	}
	
	else if ( !stricmp( argv[0], "CHECK" )|| !stricmp( argv[0], "CHK" )) {
		BYTE	status;
		if ( !stricmp( argv[1], "PC" ) || !stricmp( argv[1], "P" )) {
			status = CheckPC();
			Printf("\nStart checking PC signal--current %bd", status);
			while ( !RS_ready() ) {
				delay1ms(100);
				if ( status ) {
					status = CheckPC();
					if ( status ) continue;		// no change on status
					Puts("\nNo PC Signal!!!");
				}
				else {
					status = CheckPC();
					if (!status ) continue;		// no change on status
					Puts("\nPC Signal Comes in!!!");
					CheckAndSetPC();
				}
			}
		}
		else if ( !stricmp( argv[1], "DTV" ) || !stricmp( argv[1], "D" )) {
			status = CheckDVI();
			Printf("\nStart checking DVI signal--current %bd", status);
			while ( !RS_ready() ) {
				delay1ms(100);
				if ( status ) {
					status = CheckDVI();
					if ( status ) continue;		// no change on status
					Puts("\nNo DVI Signal!!!");
				}
				else {
					status = CheckDVI();
					if (!status ) continue;		// no change on status
					Puts("\nDVI Signal Comes in!!!");
					CheckAndSetDVI();
				}
			}
		}
		else if ( !stricmp( argv[1], "YUV" ) || !stricmp( argv[1], "Y" )) {
			status = CheckYUV();
			Printf("\nStart checking YUV signal--current %bd", status);
			while ( !RS_ready() ) {
				delay1ms(100);
				if ( status ) {
					status = CheckYUV();
					if ( status ) continue;		// no change on status
					Puts("\nNo Component Signal!!!");
				}
				else {
					status = CheckYUV();
					if (!status ) continue;		// no change on status
					Puts("\nComponent Signal Comes in!!!");
					CheckAndSetYUV();
				}
			}
		}
	}
	
	else if ( !stricmp( argv[0], "MOVE" )|| !stricmp( argv[0], "MV" )) {
		BYTE	x, y;
		BYTE x2,y2;	//WORD	result;
		if ( !stricmp( argv[1], "PC" ) || !stricmp( argv[1], "P" )) {
			if ( argc < 4 ) {
				Puts("\nMove PC x(0~100) y(0~100)");
			}
			x = a2i( argv[2] );
			y = a2i( argv[3] );
			x2 = MovePCPositionX( x );
			y2 = MovePCPositionY( y );
			Printf("\nMoved to %d, %d", x2, y2);

		}
		else if ( !stricmp( argv[1], "DTV" ) || !stricmp( argv[1], "D" )) {
		}
	}
	
	else if ( !stricmp( argv[0], "PC" )|| !stricmp( argv[0], "AUTO" )) {
			//I2CDeviceInitialize( DataInitADC );
			delay1ms(500);
			CheckAndSetPC();
	}
	
	else if ( !stricmp( argv[0], "DTV" )|| !stricmp( argv[0], "DVI" )) {
			Printf("\nDVI Input preiod: %d", CheckAndSetDVI());
	}
	
	else if ( !stricmp( argv[0], "YUV" )|| !stricmp( argv[0], "COMPONENT" )) {
			CheckAndSetYUV();
	}
	
	else if ( !stricmp( argv[0], "PHASE" )|| !stricmp( argv[0], "PH" )) {
		AutoTunePhase();
	}
	
	else if ( !stricmp( argv[0], "COLOR" )) {
		AutoTuneColor();
	}
	else if ( !stricmp( argv[0], "PCLK" )) {
		PCLKAdjust();
	}

	//---------------- Debug Level ---------------------
	else if ( !stricmp( argv[0], "DEBUG" ) ) {
		if( argc==2 ) {
			DebugLevel = a2h(argv[1]);
		}
		Printf("\nDebug Level = %2bx", DebugLevel);
	}
	
	//---------------- Echo back on/off -----------------
	else if ( !stricmp( argv[0], "echo" ) ) {
		if( !stricmp( argv[1], "off" ) ) {
			echo = 0;
			Printf("\necho off");
		}
		else {
			echo = 1;
			Printf("\necho on");
		}
	}
	//---------------- Echo back on/off -----------------
	else if ( !stricmp( argv[0], "ACCESS" ) ) {
		if( !stricmp( argv[1], "0" ) ) {
			access = 0;
			Printf("\nAccess off");
		}
		else {
			access = 1;
			Printf("\nAccess on");
		}
	}
	else if ( !stricmp( argv[0], "task" ) ) {
		if( !stricmp( argv[1], "on") ) {
			tic_task = 10*100;	// NOSIGNAL_TIME_INTERVAL
			TaskSetNoSignalCmd(TASK_CMD_RUN);
		}
		else {
			TaskSetGrid(0);
	
			TaskSetNoSignalCmd(TASK_CMD_DONE);
			WriteTW88Page( PAGE0_GENERAL );
			//WriteTW88( 0x03, 0xFF );	// disable all INTR
		}
	}
	//---------------- System Clock Display -----------------
	else if ( !stricmp( argv[0], "time" ) ) {
			Printf("\nSystem Clock: %ld:%5bd", SystemClock, tic01);
	}

	//---------------- Init ------------------------------
	else if( !stricmp( argv[0], "init" ) ) {
		//InitCVBS();
		InitWithNTSC_NEW();	// PowerUpPanelWithDefault();
		FP_GpioDefault();
		DCDC_StartUP();
		ScalerScreenBlank(OFF);
		LedPowerUp();

		if( argc>=2 ) {
			if     ( toupper(argv[1][0])=='H' ); ClockHigh();
			else if( toupper(argv[1][0])=='L' ); ClockLow();
			else                               ; Clock27();
		}
		else                                   ; Clock27();
	}

	//---------------- MCU Debug -------------------------
	else if( !stricmp( argv[0], "MCU" ) ) {
		MonitorMCU();
	}
	else if( !stricmp( argv[0], "panel" ) ) {
		InitWithNTSC_NEW();	// PowerUpPanelWithDefault();
		FP_GpioDefault();
		DCDC_StartUP();
		ScalerScreenBlank(OFF);
		LedPowerUp();
	}
	//---------------- TOUCH Debug -------------------------
#ifdef SUPPORT_TOUCH
	else if( !stricmp( argv[0], "TOUCH" ) ) {
		if ( argc == 2 ) {
			if ( !stricmp( argv[1], "ON" ) ) {
				ET1 = 1;	//BK110531
				SetTouchAuto(1);
				Puts("\r\nTurn ON Touch Sense");
			}
			else if ( !stricmp( argv[1], "DATA" ) ) {
				PrintCalibData();
			}
			else {
				SetTouchAuto(0);
				ET1 = 0;	//BK110531
				Puts("\r\nTurn OFF Touch Sense");
			}
		}
		else {
			GetTouch();
		}
	}
	else if( !stricmp( argv[0], "TRACE" ) ) {
		if ( argc == 2 ) {
			if ( !stricmp( argv[1], "ON" ) ) {
				Puts("\r\nTurn ON Touch Trace");
				ET1 = 0;	//BK110531
				SetTraceAuto(1);
			}
			else {
				Puts("\r\nTurn OFF Touch Trace");
				ET1 = 1;	//BK110531
				SetTraceAuto(0);
			}
		}
		else {
//			TraceTouch();
			extern 	bit FLAG_TOUCH;
			extern	WORD	TouchX, TouchY, Z1, AUX[4];
			extern	BYTE	TouchChanged, AUX_Changed[4];
			BYTE	temp, atemp[4], i;
			temp = TouchChanged;
			atemp[0] = AUX_Changed[0];
			atemp[1] = AUX_Changed[1];
			atemp[2] = AUX_Changed[2];
			atemp[3] = AUX_Changed[3];
			while ( !RS_ready() ) {
				delay1ms(500);
				if ( FLAG_TOUCH ) {
					if (TouchChanged != temp) {
						Printf("\nTouchX: 0x%04x, TouchY: 0x%04x, Z1: 0x%04x", TouchX, TouchY, Z1);
						temp = TouchChanged;
					}
				}
				for ( i=0; i<4; i++ ) {
					if ( atemp[i] != AUX_Changed[i] ) {
						Printf("\n0x%02bx - AUX[%bd]: 0x%04x",atemp[i], i,  AUX[i]);
						atemp[i] = AUX_Changed[i];
					}
				}
			}
		}
	}
	else if( !stricmp( argv[0], "TDUMP" ) ) {
		Puts("\r\nTouch Dump");
		TouchDump();
	}
	
	//---------------- Touch Calibration -------------------------
	else if( !stricmp( argv[0], "CALIB" ) ) {
		BYTE	n;
		if ( argc == 2 ) {
			n = a2h(argv[1]);
			if ( n > 4 ) n = 4;
			CalibTouch(n);
		}
	}
#endif
	//---------------- Delta RGB Panel Test -------------------------
	#ifdef SUPPORT_DELTA_RGB

	else if( !stricmp( argv[0], "delta" ) ) {
		extern	BYTE	SPI_ID;
		if( argc==1 )	DeltaRGBPanelInit();
		else {
			
			if( !stricmp( argv[1], "w" ) ) {

				WORD val;
				//val= Asc2Bin(argv[3])<<8 | Asc2Bin(argv[4]);
				val= a2h(argv[3]);
				Printf("\r\n SPI Write: Addr:%2x  Data:%4x", (WORD)a2h(argv[2]), val);
				WriteSPI(a2h(argv[2]), val );
			}
			else if( !stricmp( argv[1], "r" ) ) {
				WORD val;
				val = ReadSPI(a2h(argv[2]));
				Printf("\r\n SPI Read: Addr:%2x  Data:%4x", (WORD)a2h(argv[2]), val);
			}
			else if( !stricmp( argv[1], "c" ) ) {
				if ( argc == 3 ) 
					SPI_ID = a2h(argv[2]) & 0xfc;
				Printf("\r\n SPI Device ID:%2bx", SPI_ID);
			}
		}
	}
	else if( !stricmp( argv[0], "AUO" ) ) {
		if( argc==1 )	Puts( "\r\nIncorrect commad - AUO r ii or AUO w ii ddd" );
		else {
			
			if( !stricmp( argv[1], "w" ) ) {

				WORD val;
				val= a2h(argv[3]);
				Printf("\r\n SPI Write: Addr:%2x  Data:%4x", (WORD)a2h(argv[2]), val);
				WriteAUO(a2h(argv[2]), val );
			}
			else if( !stricmp( argv[1], "r" ) ) {
				WORD val;
				val = ReadAUO(a2h(argv[2]));
				Printf("\r\n SPI Read: Addr:%2x  Data:%4x", (WORD)a2h(argv[2]), val);
			}
		}
	}
	else if( !stricmp( argv[0], "AUO2" ) ) {
		if( argc==1 )	Puts( "\r\nIncorrect commad - AUO2 r ii or AUO2 w ii ddd" );
		else {
			
			BYTE val, addr;
			if( !stricmp( argv[1], "w" ) ) {

				addr = a2h( argv[2] );
				val= a2h( argv[3] );
				Printf("\r\n SPI Write: Addr:%02bx  Data:%02bx", addr, val);
				WriteAUO2( addr, val );
			}
			else if( !stricmp( argv[1], "r" ) ) {
				addr = a2h( argv[2] );
				val = ReadAUO2( addr );
				Printf("\r\n SPI Read: Addr:%02bx  Data:%02bx", addr, val);
			}
		}
	}
	#endif
	
	//---------------- SPI Debug -------------------------
	else if( !stricmp( argv[0], "SPI" ) ) {
		MonitorSPI();
	}
	else if( !stricmp( argv[0], "SPIC" ) ) {
		MonitorSPIC();
	}
	
	else if( !stricmp( argv[0], "EE" ) ) {
		MonitorEE();
	}

	//---------------- MENU Debug -------------------------
	else if( !stricmp( argv[0], "menu" ) ) {
		MonitorMenu();
	}
	//---------------- Font Osd Debug -------------------------
	else if( !stricmp( argv[0], "fosd" ) ) {
		MonitorFOsd();
	}
	//---------------- SPI Osd Debug -------------------------
	else if( !stricmp( argv[0], "sosd" ) ) {
		MonitorSOsd();
	}
	//---------------- SPI OSD Test -------------------------
	else if( !stricmp( argv[0], "spiosd6" ) ) {
		LoadMenu(6,0x710400,0,0,800,64);
		OsdWinEnable(6, 1);
	}
	//---------------- SPI OSD Test -------------------------
	else if( !stricmp( argv[0], "spiosd7" ) ) {
		LoadMenu(7,0x71cc00,0x10,0,111,64);
		OsdWinEnable(7, 1);
	}
	//----------------------------------------------------
    else if( !stricmp( argv[0], "lut" ) ) {
#if 0    
        SpiOsdIoLoadLUT(a2h(argv[1]),a2h(argv[2]), 0x400, 0x710000);
#else    
        if ( !stricmp( argv[1], "0" ) ) 
				SpiOsdLoadLUT(0, 0x00, 0x400, 0x710000);
		else if( !stricmp( argv[1], "1" ) ) 
				SpiOsdLoadLUT(1, 0x00, 0x400, 0x710000);
		else if( !stricmp( argv[1], "2" ) ) 
				SpiOsdLoadLUT(2, 0x00, 0x400, 0x710000);
		else if( !stricmp( argv[1], "3" ) ) 
				SpiOsdLoadLUT(3, 0x00, 0x400, 0x710000);
#endif		
	}
#endif	
	else {
		Printf("\nInvalid command...");
	}


	Prompt();


}

//=============================================================================
//  UART1. 57600bps 8Data 1Stop NoParity NoFlowControl
//=============================================================================

#ifdef SUPPORT_UART1
BYTE *Mon1String = 0;
void ProcessFinishMessage(void)
{
	Puts1("OK");
}

void Monitor1(void)
{
    BYTE cmd;
#if 0
	//test routine. - echo
	BYTE	ch;

	if( !RS1_ready() ) return;
	ch = RS1_rx();					// get data
	RS1_tx(ch);		   				// echo back
#endif

	if( Mon1String ) {
		RS1_ungetch( *Mon1String++ );
		if( *Mon1String==0 ) Mon1String = 0;
	}

	if( !Mon1GetCommand() ) return;

	//---------------- Get TW8835 Info -------------------
	if( !stricmp( argv1[0], "GE" ) ) {
		if(argc1 < 2)
			return;
		cmd = a2h( argv1[1] );
		switch(cmd) {
		case 0x01:	//get FW revision
			_outchar1((BYTE)(FWVER>>8));
			_outchar1((BYTE)FWVER);
			ProcessFinishMessage();
			break;
		}	
	}
	
	//---------------- Read data from audio -------------------
	else if( !stricmp( argv1[0], "RA" ) ) {
		if(argc1 < 2)
			return;
	}
	//---------------- Read data to audio amplifier -------------------
	else if( !stricmp( argv1[0], "WA" ) ) {
		if(argc1 < 2)
			return;
	}
	//---------------- Help -----------------------------
	else if( !stricmp( argv1[0], "H" ) || !stricmp( argv1[0], "HELP" ) || !stricmp( argv1[0], "?" ) ) {
		Mon1Help();
		ProcessFinishMessage();
	}

}
#endif






